<template>
	<view>
		<navbar :isback="true" :opc='opc' :title="title" :placeholder="false" @clickright="clickright"
			righticon='/static/msg/tp.png' class="navbar"></navbar>
			
			
		<u-loading-page icon-size="100rpx" :loading="loading" loading-text="驼乡生活信息超市"></u-loading-page>
		<view class="" v-if="!loading">
			<image :src="userinfo.background_image?baseurl+userinfo.background_image:'/static/msg/bg.png'"
				@click="preview([userinfo.background_image?baseurl+userinfo.background_image:'/static/msg/bg.png'],0)" class="topbg" mode="aspectFill"></image>
			<view class="con">
				<view class="con__top">
					<view class="u-flex u-flex-y-center">
						<u-avatar :src="URL(userinfo.avatar)" shape="circle" size="130rpx"></u-avatar>
						<view class="con__top__center">
							<view class="con__top__nickname">
								{{userinfo.username}}
							</view>
							<view class="con__top__addr" v-if="userinfo.address">
								ID:{{userinfo.id}}
							</view>
						</view>
					</view>
					<u-icon @click="callphone(userinfo.mobile)" name="/static/msg/mobile2.png" size="82rpx"></u-icon>
				</view>
			</view>
			<view class="msglist">
				<msgitem v-for="(item,index) in msglist" :key="index" :item="item"></msgitem>
			</view>
			<u-safe-bottom></u-safe-bottom>
		</view>
		<view class="kf">
			<button class="u-reset-button" open-type="contact">
				<image src="/static/xishi/kf.png" class="kficon" mode=""></image>
			</button>
		</view>
	</view>
</template>

<script>
	import { BASE_URL } from '@/api/config';
	import user from '@/api/_user.js'
	export default {
		data() {
			return {
				title: '',
				opc: 0,
				topbgh: 0,
				msglist: [],
				userinfo: {},
				baseurl:BASE_URL,
				userId:'',
				loading:true
			};
		},
		onLoad(o) {
			this.title = o.title
			this.userId=o.userId
			this.getlist()
		},
		onShow() {
			this.getlist()
		},
		onPageScroll(e) {
			this.opc = e.scrollTop / (this.topbgh)
		},
		methods: {
			getlist(){
				let o=this
				if(o.userId){
					user.getUserInfo({id:o.userId}).then(res=>{
						if(res.data.code==1){
							this.userinfo=res.data.data
						}
					})
					user.findInfo({userId:o.userId,status:2}).then(res1=>{
						if(res1.data.code==1){
							this.loading=false
							this.msglist=res1.data.data
							this.msglist.map(t=>{
								t.imags=[]
								t.imag=t.ad_images.split(',')
								if (t.imag[0] == '') {
									t.imag = []
								}
								t.imag.map(t1=>{
									t.imags.push(BASE_URL+t1)
								})
							})
							this.$forceUpdate()
						}
					})
				}else{
					this.userinfo = this.$store.state.userinfo
					user.myInfo({status:2}).then(res1=>{
						if(res1.data.code==1){
							this.loading=false
							this.msglist=res1.data.data
							this.msglist.map(t=>{
								t.imags=[]
								t.imag=t.ad_images.split(',')
								if (t.imag[0] == '') {
									t.imag = []
								}
								t.imag.map(t1=>{
									t.imags.push(BASE_URL+t1)
								})
							})
							this.$forceUpdate()
						}
					})
				}
			},
			getElInfo() {
				let _this = this
				const query = uni.createSelectorQuery().in(this);
				query
					.select(".topbg")
					.boundingClientRect((data) => {
						_this.topbgh = data.height - 44 - uni.$u.sys().statusBarHeight
					})
					.exec();
			},
			clickright() {
				let _this=this
				uni.chooseImage({
					success: (chooseImageRes) => {
						const tempFilePaths = chooseImageRes.tempFilePaths;
						uni.showLoading()
						uni.uploadFile({
							url: BASE_URL + '/api/Common/upload',
							filePath:  tempFilePaths[0],
							name: "file",
							header: {
								"token": uni.getStorageSync('xxcstoken')
							},
							success: function(res) {
								uni.hideLoading()
								if (JSON.parse(res.data).code == 1) {
									
									_this.userinfo.background_image = JSON.parse(res.data).data.url
									user.profile({background_image:_this.userinfo.background_image}).then(res => {
										if (res.data.code == 1) {
											user.userinex().then(r => {
												if (r.data.code == 1) {
													uni.$u.toast('上传成功')
													_this.$store.commit('getuserinfo', r.data.data)
													// uni.navigateBack()
												}
											})
										}
									})
								} else {
									uni.$u.toast('上传失败')
								}
							},
							error: function(rev) {
								console.log(rev);
							}
						});
						// uni.uploadFile({
						// 	url: BASE_URL+'/api/Common/upload', //仅为示例，非真实的接口地址
						// 	filePath: tempFilePaths[0],
						// 	name: 'background_image',
						// 	header:{
						// 		token:uni.getStorageSync('xxcstoken')
						// 	},
						// 	formData: {
						// 		'user': _this.$store.state.userinfo.id
						// 	},
						// 	success: (uploadFileRes) => {
						// 		console.log(uploadFileRes.data);
						// 	}
						// });
					}
				});
			}
		},
		mounted() {
			this.getElInfo()
		}
	}
</script>

<style lang="scss">
	.topbg {
		width: 750rpx;
		height: 661rpx;
	}

	.con {
		background: #FFFFFF;
		border-radius: 30rpx 30rpx 0 0;
		margin-top: -30rpx;
		position: relative;

		&__top {
			padding: 40rpx 34rpx 34rpx 28rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			border-bottom: 1rpx solid #ECECEC;

			&__center {
				padding: 20rpx 0 12rpx 20rpx;
				height: 98rpx;
				display: flex;
				flex-direction: column;
				justify-content: space-between;

			}

			&__nickname {
				font-family: PingFang SC;
				font-weight: bold;
				font-size: 36rpx;
				color: #1E1E1E;
				line-height: 36rpx;
			}

			&__addr {
				padding: 0 10rpx;
				height: 49rpx;
				background: #ECECEC;
				border-radius: 10rpx;
				font-family: PingFang SC;
				font-weight: 500;
				font-size: 26rpx;
				color: #6C6C6B;
				line-height: 49rpx;
			}
		}
	}
</style>